package com.pxx.service.controller;

import com.item.common.PageResult;
import com.pxx.item.pojo.Brand;
import com.pxx.service.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌控制层
 */
@RestController
@RequestMapping("/brand")
public class BrandController {


    @Autowired
    private BrandService brandService;

    /**
     *
     * 根据查询条件分页并且排序查询品牌信息
     *
     * key 查询的条件
     * page 当前页
     * rows 条数
     * sortBy 排序字段名称
     * desc  是否倒叙
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "key",required = false)String key ,
            @RequestParam(value = "page",defaultValue = "1")Integer page ,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows ,
            @RequestParam(value = "sortBy",required = false)String sortBy ,
            @RequestParam(value = "desc",required = false)Boolean desc

    ){
        PageResult<Brand> result  = brandService.queryBrandByPage(key,page,rows,sortBy,desc);
        if (CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(result);
    }


    /***
     * 新增品牌
     * @param brand 品牌对象
     * @param cids  商品分类和品牌的关系
     * @return
     */
    @PostMapping
    public ResponseEntity<Void>  saveBrand(Brand brand, @RequestParam("cids")List<Long> cids){
        brandService.saveBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 根据商品分类的id查询绑定的品牌列表
     * @param cid
     * @return
     */
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid")Long cid){
        List<Brand>  brandList =    brandService.queryBrandByCid(cid);
        if (CollectionUtils.isEmpty(brandList)){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(brandList);

    }



    @GetMapping("/getBrandInfo/{brandId}")
    public ResponseEntity<Brand> getBrandInfo(@PathVariable("brandId")Long brandId){
      Brand brand  =  brandService.queryBrandInfoById(brandId);
        return ResponseEntity.ok(brand);
    }


}
